package com.china_goldcard.loadingdialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by jk on 16/10/24.
 */
public class LoadingEmptyView  extends RelativeLayout implements View.OnClickListener  {

    public interface ReloadBtnOnClickListener{
        void onClicked(View view);
    }

    private String errorMsg = "暂无数据";

    private String mLoadingText = "加载中...";

    private String reloadBtnTitle = "重新加载";

    private View errorView;

    private LoadingDialog loadingDialog;

    private TextView errorTip;

    private Button reloadBtn;

    private View mBindView;

    private ReloadBtnOnClickListener onBtnClickListener;

    public LoadingEmptyView(Context context) {
        super(context);
    }

    public LoadingEmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public LoadingEmptyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    /**
     * 初始化
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingEmptyView, 0, 0);

        errorMsg = typedArray.getString(R.styleable.LoadingEmptyView_empty_warn_txt);

        reloadBtnTitle = typedArray.getString(R.styleable.LoadingEmptyView_empty_button_txt);

        mLoadingText = typedArray.getString(R.styleable.LoadingEmptyView_empty_loading_txt);

        typedArray.recycle();

        // 错误视图
        errorView = inflate(context,R.layout.loading_empty_error_view,null);
        errorView.setVisibility(View.GONE);
        errorTip = (TextView) errorView.findViewById(R.id.empty_view_errorv);
        reloadBtn = (Button) errorView.findViewById(R.id.empty_view_reload_btn);
        reloadBtn.setOnClickListener(this);
        reloadBtn.setText(reloadBtnTitle);
        LayoutParams mLoadingDataLp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mLoadingDataLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(errorView,mLoadingDataLp);

        setVisibility(GONE);
    }

    /**
     * 加载
     */
    public void loading(){
        if (mBindView != null){
            mBindView.setVisibility(GONE);
        }
        errorView.setVisibility(GONE);
        setVisibility(GONE);
        showLoadingDialog();
    }

    /**
     * 显示加载框
     */
    private void showLoadingDialog(){
        post(new Runnable() {
            @Override
            public void run() {
                loadingDialog = new LoadingDialog(getContext(),false);
                loadingDialog.setLoadingText(mLoadingText);
                loadingDialog.show();
            }
        });
    }

    /**
     * 隐藏加载框
     */
    private void hideLoadingDialog(){
        post(new Runnable() {
            @Override
            public void run() {
                loadingDialog.dismiss();
                loadingDialog = null;
            }
        });
    }

    /**
     * 成功
     */
    public void success(){
        if (mBindView != null){
            mBindView.setVisibility(VISIBLE);
        }
        hideLoadingDialog();
        setVisibility(GONE);
    }

    /** 设置绑定view
     * @param view
     */
    public void bindView(View view){
        this.mBindView = view;
    }

    /**
     * 错误
     * @param message
     */
    public void error(String message){
        this.errorMsg = message;
        hideLoadingDialog();
        setVisibility(VISIBLE);
        if (mBindView != null){
            mBindView.setVisibility(GONE);
        }
        errorView.setVisibility(VISIBLE);
        errorTip.setText(errorMsg);
    }

    public void setReloadBtnTitle(String reloadBtnTitle) {
        this.reloadBtnTitle = reloadBtnTitle;
    }

    public void setmLoadingText(String mLoadingText) {
        this.mLoadingText = mLoadingText;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public void setOnBtnClickListener(ReloadBtnOnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    @Override
    public void onClick(View v) {
        if (onBtnClickListener != null){
            this.onBtnClickListener.onClicked(v);
        }
    }
}
